package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase {
	
	public static boolean CREATION = true;
	public static boolean MODIFICATION = false;
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cachedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cachedContacts == null) {
			rebuildCache();
		}
		return cachedContacts;
	}
	
	private void rebuildCache() {
		cachedContacts = new SortedListOf<ContactData>();
		
		manager.navigateTo().mainPage();
		List<WebElement> rows = driver.findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
			List<WebElement> cells = row.findElements(By.tagName("td"));
			ContactData contact = new ContactData();
			contact.setFirstName(cells.get(1).getText());
			contact.setLastName(cells.get(2).getText());
			cachedContacts.add(contact);
		}	
	}

	public ContactHelper createContact(ContactData contact) {
		initNewContactCreation();
		fillContactForm(contact, CREATION);
	    submitContactCreation();
	    returnToHomePage();
	    rebuildCache();
	    return this;
	}
	
	public ContactHelper deleteContact(int i) {
		submitContactDeletion();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData contact) {
		gotoEditPage(index);
		fillContactForm(contact, MODIFICATION);
		submitContactModification();
		returnToHomePage();
		rebuildCache();
		return this;
	}
	
	//-------------------------------------------------------------------------------------
	
	public ContactHelper initNewContactCreation() {
	    click(By.linkText("add new"));
	    return this;
	}
	
	public ContactHelper fillContactForm(ContactData contact, boolean formType) {
		type(By.name("firstname"), contact.getFirstName());
		type(By.name("lastname"), contact.getLastName());
		type(By.name("address"), contact.getFirstAddress());
		type(By.name("home"), contact.getFirstHome());
		type(By.name("mobile"), contact.getMobile());
		type(By.name("work"), contact.getWork());
		type(By.name("email"), contact.getFirstEmail());
		type(By.name("email2"), contact.getSecondEmail());
	    selectByText(By.name("bday"), contact.getBirthDay());
	    selectByText(By.name("bmonth"), contact.getMonth());
	    type(By.name("byear"), contact.getYear());
	    if (formType == CREATION) {
	    selectByText(By.name("group"), contact.group);
	    } else {
	    	if (((List<WebElement>) driver.findElement(By.name("new_group"))).size() != 0) {
	    	throw new Error("Group selector exists in contact modification form");
	    	}
	    }
	    type(By.name("address2"), contact.getSecondAddress());
	    type(By.name("phone2"), contact.getSecondHome());
	    return this;
	}
	
	public ContactHelper submitContactCreation() {
	    click(By.name("submit"));
	    return this;
	}

	public ContactHelper returnToHomePage() {
	    click(By.linkText("home page"));
	    return this;
	}
	
	public ContactHelper gotoEditPage(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
		return this;
	}

	public ContactHelper submitContactModification() {
		click(By.xpath("(//input[@name='update'])[1]"));
		return this;
	}

	public void submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}
}

